/*     */ package com.zimbra.cs.dav.service.method;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.Depth;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.LockMgr;
/*     */ import com.zimbra.cs.dav.LockMgr.Lock;
/*     */ import com.zimbra.cs.dav.LockMgr.LockScope;
/*     */ import com.zimbra.cs.dav.LockMgr.LockType;
/*     */ import com.zimbra.cs.dav.property.LockDiscovery;
/*     */ import com.zimbra.cs.dav.service.DavMethod;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import java.io.IOException;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Lock
/*     */   extends DavMethod
/*     */ {
/*     */   public static final String LOCK = "LOCK";
/*     */   
/*     */   public String getName()
/*     */   {
/*  43 */     return "LOCK";
/*     */   }
/*     */   
/*     */   public void handle(DavContext ctxt) throws DavException, IOException, ServiceException {
/*  47 */     LockMgr lockmgr = LockMgr.getInstance();
/*  48 */     LockMgr.Lock lock = null;
/*  49 */     if (ctxt.hasRequestMessage()) {
/*  50 */       DavContext.Depth depth = ctxt.getDepth();
/*  51 */       if (depth == DavContext.Depth.one)
/*  52 */         throw new DavException("invalid depth", 400, null);
/*  53 */       String d = depth == DavContext.Depth.zero ? "0" : depth.toString();
/*     */       
/*  55 */       LockMgr.LockScope scope = LockMgr.LockScope.shared;
/*  56 */       LockMgr.LockType type = LockMgr.LockType.write;
/*     */       
/*  58 */       Document req = ctxt.getRequestMessage();
/*  59 */       Element top = req.getRootElement();
/*  60 */       if (!top.getName().equals("lockinfo")) {
/*  61 */         throw new DavException("msg " + top.getName() + " not allowed in LOCK", 400, null);
/*     */       }
/*  63 */       Element e = top.element(DavElements.E_LOCKSCOPE);
/*     */       
/*  65 */       List<Element> ls = e.elements();
/*  66 */       for (Element v : ls) {
/*  67 */         if (v.getQName().equals(DavElements.E_EXCLUSIVE)) {
/*  68 */           scope = LockMgr.LockScope.exclusive;
/*  69 */         } else if (v.getQName().equals(DavElements.E_SHARED)) {
/*  70 */           scope = LockMgr.LockScope.shared;
/*     */         } else
/*  72 */           throw new DavException("unrecognized scope element " + v.toString(), 400, null);
/*     */       }
/*  74 */       e = top.element(DavElements.E_LOCKTYPE);
/*     */       
/*  76 */       List<Element> lt = e.elements();
/*  77 */       for (Element v : lt) {
/*  78 */         if (v.getQName().equals(DavElements.E_WRITE)) {
/*  79 */           type = LockMgr.LockType.write;
/*     */         } else {
/*  81 */           throw new DavException("unrecognized type element " + v.toString(), 400, null);
/*     */         }
/*     */       }
/*  84 */       e = top.element(DavElements.E_OWNER);
/*  85 */       String owner; String owner; if ((e != null) && (e.elementIterator(DavElements.E_HREF).hasNext())) {
/*  86 */         Element ownerElem = (Element)e.elementIterator(DavElements.E_HREF).next();
/*  87 */         owner = ownerElem.getText();
/*     */       } else {
/*  89 */         owner = ctxt.getAuthAccount().getName();
/*     */       }
/*     */       
/*  92 */       lock = lockmgr.createLock(ctxt, owner, ctxt.getUri(), type, scope, d);
/*  93 */       ctxt.getResponse().addHeader("Lock-Token", lock.toLockTokenHeader());
/*     */     } else {
/*  95 */       String token = ctxt.getRequest().getHeader("If");
/*  96 */       if (token == null) {
/*  97 */         throw new DavException("no request body", 400, null);
/*     */       }
/*     */       
/* 100 */       token = token.trim();
/* 101 */       int len = token.length();
/* 102 */       if ((token.charAt(0) == '(') && (token.charAt(len - 1) == ')')) {
/* 103 */         token = token.substring(1, len - 1);
/*     */       }
/* 105 */       List<LockMgr.Lock> locks = lockmgr.getLocks(ctxt.getUri());
/* 106 */       for (LockMgr.Lock l : locks) {
/* 107 */         if (l.token.equals(LockMgr.Lock.parseLockTokenHeader(token))) {
/* 108 */           l.extendExpiration();
/* 109 */           lock = l;
/* 110 */           break;
/*     */         }
/*     */       }
/* 113 */       if (lock == null) {
/* 114 */         throw new DavException("Lock does not exist", 412, null);
/*     */       }
/*     */     }
/*     */     
/* 118 */     ctxt.getDavResponse().addProperty(ctxt, new LockDiscovery(lock));
/* 119 */     ctxt.setStatus(200);
/* 120 */     sendResponse(ctxt);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/method/Lock.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */